package com.app.micaihu.view.main.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;

import com.android.volley.VolleyError;
import com.app.micaihu.R;
import com.app.micaihu.adapter.VideoRankAdapter;
import com.app.micaihu.app.AppApplication;
import com.app.micaihu.base.BaseOldActivity;
import com.app.micaihu.base.LazyBaseListFragment;
import com.app.micaihu.base.bean.DataBean;
import com.app.micaihu.bean.news.VideoRankEntity;
import com.app.micaihu.configure.Config;
import com.app.micaihu.configure.TypeConstant;
import com.app.micaihu.configure.URIContant;
import com.app.micaihu.listener.HttpListener;
import com.app.micaihu.view.newsdetail.VideoDetailActivity;
import com.baidu.mobstat.StatService;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Guo on 2017/5/22.
 *
 *
 *
 * 视频排行榜
 */

public class VideoRankingFragment extends LazyBaseListFragment<VideoRankEntity> {

    private String type;//排行榜分类，1点播榜 2热评榜 3月度榜

    private HashMap<String,String> listMap = new HashMap<>();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        if(getArguments() != null){
//            type = getArguments().getString(PARAMETER1);
//
//        }
    }

    @Override
    protected void changeData(final boolean isRefresh) {
        Map<String, String> params = new HashMap<>();


//        params.put("type", type);
        params.put("page", dataPage + "");

        HttpListener<DataBean<List<VideoRankEntity>>> listListener = new HttpListener<DataBean<List<VideoRankEntity>>>() {
            @Override
            public void onStart() {
                super.onStart();
                setLoadView(LOADING, null);
            }

            @Override
            public void onError(VolleyError error) {
                setLoadView(NO_NET, AppApplication.getInstance().getString(R.string.neterror_click));
            }

            @Override
            public void onSuccess(DataBean<List<VideoRankEntity>> response) {
                if (response.noError() && response.getData() != null) {
                    List<VideoRankEntity> videoList = response.getData();
                    if (videoList.size() > 0) {
                        if (dataList == null) {
                            dataList = new ArrayList<VideoRankEntity>();
                        }
                        if (isRefresh) {
                            //清空数据
                            dataList.clear();
                            listMap.clear();
                        }
                        dataRevisited(videoList);
                        dataList.addAll(videoList);
                        if (mAdapter == null) {
                            mAdapter = new VideoRankAdapter(dataList, getActivity());
                            mListView.setAdapter(mAdapter);
                        } else {
                            mAdapter.notifyDataSetChanged();
                        }
                        setLoadView(LOADSUCES, null);
                    } else {
                        if (isRefresh && dataList != null) {
                            dataList.clear();
                            listMap.clear();
                        }
                        if (dataList == null || dataList.size() == 0) {
                            setLoadView(DATAEMPTY, TypeConstant.EMPTYVIEW_DEFAULT, getResources().getString(R.string.pullup_no_data));
                        } else {
                            setLoadView(LOADSUCES, getString(R.string.homepage_msg_nomoremsg));
                        }
                    }

                } else {
                    setLoadView(DATAERROR, response.getNnderstoodMsg());
                }
            }
        };

        addPostCacheRequest(URIContant.VIDEO_RANKING, new TypeToken<DataBean<List<VideoRankEntity>>>() {}.getType(), params, listListener);

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //因为有头部
        if(mListView.getRefreshableView().getHeaderViewsCount() > 0){
            position = position - mListView.getRefreshableView().getHeaderViewsCount();
        }
        if(position >= dataList.size()){
            position = dataList.size() - 1;
        }else if(position < 0){
            position = 0;
        }
        StatService.onEvent(activity, "055", "视频榜_视频", 1);// 统计的点击的次数

        VideoRankEntity videoRankEntity = dataList.get(position);
        if(videoRankEntity == null) return;
        Intent intent = new Intent();
        intent.putExtra(Config.Extra.PARAMETER1,videoRankEntity.getArticleId());
        intent.setClass(activity, VideoDetailActivity.class);
        startActivity(intent);
    }


    /**
     * 数据去重
     *
     * @param list
     */
    private void dataRevisited(List<VideoRankEntity> list) {
        if (list == null || list.size() == 0) {
            return;
        }
        for (int i = 0; i < list.size(); i++) {
            if (i >= list.size()) {
                break;
            }
//            GameCenterEntity gameCenterEntity = list.get(i);
//            if (gameCenterEntity == null) {
//                continue;
//            }
//            //开始去重计算
//            if (listMap.get(gameCenterEntity.getPackageName()) == null) {
//                listMap.put(gameCenterEntity.getPackageName(), gameCenterEntity.getPackageName());
//            } else {
//                list.remove(i);//如果包含则替换
//                i--;
//
//            }
        }
    }
}
